package com.github.utils;

import java.util.*;

/**
 * <p>集合工具类</p>
 * @author  Crab2Died
 */
public class CollectionUtils {

    /**
     * <p>集合是否为空判断</p>
     * @author  Crab2Died
     */
    static
    public boolean isEmpty(Collection<?> collection) {

        return collection == null || collection.isEmpty();
    }

    /**
     * <p>{@link Map}是否为空判断</p>
     * @author  Crab2Died
     */
    static
    public boolean isEmpty(Map<?, ?> map) {

        return map == null || map.isEmpty();
    }

    /**
     * <p>数组是否为空判断</p>
     * @author  Crab2Died
     */
    static
    public boolean isEmpty(Object[] array) {

        return array == null || array.length <= 0;
    }

    /**
     * <p>数组转为Collection集合</p>
     * @author  Crab2Died
     *
     * @param array 数组
     * @return {@link Collection<E>}
     */
    static
    public <E> Collection<E> array2Collection(E[] array) {

        if (isEmpty(array))
            return null;
        return Arrays.asList(array);
    }

    /**
     * <p>数组转为List集合</p>
     * @author  Crab2Died
     *
     * @param array 数组
     * @return {@link List<E>}
     */
    static
    public <E> List<E> array2List(E[] array) {

        return (List<E>) array2Collection(array);
    }

    /**
     * <p>Collection集合转数组</p>
     * @author  Crab2Died
     *
     * @param collection 集合
     * @return 数组
     */
    @SuppressWarnings("unchecked")
    static
    public <E> E[] Collection2Array(Collection<E> collection) {

        if (isEmpty(collection))
            return null;
        return (E[]) collection.toArray();
    }

    /**
     * <p>合并数组</p>
     * @author  Crab2Died
     *
     * @param head  待合并第一个数组
     * @param cells 带合并的其他数组
     * @return 合并后数组
     */
    static
    public <T> T[] mergeArray(T[] head, T[]... cells) {
        int totalLength = head.length;
        for (T[] array : cells) {
            totalLength += array.length;
        }
        T[] result = Arrays.copyOf(head, totalLength);
        int offset = head.length;
        for (T[] array : cells) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }

    /**
     * <p>properties 转 map</p>
     * @author  Crab2Died
     *
     * @param props properties
     * @return {@link Map}
     */
    static
    public Map<String, Object> prop2Map(Properties props) {

        Map<String, Object> map = new HashMap<>();
        for (Enumeration<?> names = props.propertyNames(); names.hasMoreElements(); ) {
            String key = (String) names.nextElement();
            Object value = props.get(key);
            if (value == null) {
                // Allow for defaults fallback or potentially overridden accessor...
                value = props.getProperty(key);
            }
            map.put(key, value);
        }
        return map;
    }

    /**
     * <p>properties 转 map</p>
     * @author  Crab2Died
     *
     * @param props properties
     * @param map   {@link Map}
     */
    @SuppressWarnings("unchecked")
    static
    public <K, V> void prop2Map(Properties props, Map<K, V> map) {

        for (Enumeration<?> names = props.propertyNames(); names.hasMoreElements(); ) {
            String key = (String) names.nextElement();
            Object value = props.get(key);
            if (value == null) {
                // Allow for defaults fallback or potentially overridden accessor...
                value = props.getProperty(key);
            }
            map.put((K) key, (V) value);
        }
    }
}
